package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.outStorage.PickingOutStorageDetailListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStoragePickingBatchListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStoragePickingOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStoragePickingDefaultBatchRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;

import java.util.List;


//验收入库
public class OutStoragePickingDetailActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView pickingOutStorageDetailList;

    private PickingOutStorageDetailListAdapter adapter;

    private String mSerial, mBatchNumber, mPickingClassify, mUseDepartment;

    private OutStoragePickingOverviewBean.DataBean.MaterielListBean mMaterielDetail;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_picking_detail);
        initView(OutStoragePickingDetailActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(OutStoragePickingDetailActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        pickingOutStorageDetailList = (PullableListView) activity.findViewById(R.id.pickingOutStorageDetailList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.pickingOutStorageDetailRefresh);
    }

    private void initData(Activity activity) {
        title.setText("领料明细");
        ActivityUtils.initBackAndFunction(activity, "领料概览", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String message = check();
                if (message != null) {
                    DialogUtils.getConfirmDialog(OutStoragePickingDetailActivity.this, message, new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            OutStoragePickingDetailActivity.this.finish();
                        }
                    }).show();
                } else {
                    updateInfo(OutStoragePickingDetailActivity.this);
                    OutStoragePickingDetailActivity.this.finish();
                }
            }
        });

        pickingOutStorageDetailList.setPullUpFlag(false);
        pickingOutStorageDetailList.setPullDownFlag(false);
        mBatchNumber = "";
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            String type = QRCodeUtils.getQRCodeType(scanResult);
            switch (type) {
                case QRCodeConstant.TYPE_BATCH:
                    adapter.addBatchRequest(activity, false, null, QRCodeUtils.DecryptQRCode(scanResult, QRCodeConstant.PARAMETER_BATCH_NUMBER));
                    break;
                case QRCodeConstant.TYPE_POSITION:
                    adapter.addBatchRequest(activity, false, null, QRCodeUtils.DecryptQRCode(scanResult));
                    break;
            }
        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_POSITION);
    }

    private void getExtra(Activity activity) {
        mSerial = activity.getIntent().getStringExtra(OutStorageExtra.SERIAL);
        mPickingClassify = activity.getIntent().getStringExtra(OutStorageExtra.PICKING_CLASSIFY);
        mUseDepartment = activity.getIntent().getStringExtra(OutStorageExtra.USE_DEPARTMENT);
    }

    private void addOutStoragePickingItemListData(Context context, OutStoragePickingOverviewBean.DataBean.MaterielListBean materielDetail, List<OutStoragePickingBatchListBean.DataBean.BatchListBean> batchList, String batchNumber, ListView listView) {
        adapter = new PickingOutStorageDetailListAdapter(context, materielDetail, batchList, batchNumber, mPickingClassify, mUseDepartment, new OutStoragePickingDetailListener() {

            @Override
            public void getDefaultBatch(String number) {
                OutStoragePickingDefaultBatchRequest.request(context, mMaterielDetail.getMaterielCode(), number, mPickingClassify, mUseDepartment, mMaterielDetail.getFactoryCode(), mMaterielDetail.getInventoryAddressCode(), new GsonBeanListener<OutStoragePickingBatchListBean>() {
                    @Override
                    public void onSuccess(OutStoragePickingBatchListBean bean) {
                        addOutStoragePickingItemListData(context, mMaterielDetail, bean.getData().getBatchList(), "", pickingOutStorageDetailList);
                    }
                });
            }

            @Override
            public void selectItem(int position) {
                pickingOutStorageDetailList.setSelection(position);
            }
        });
        listView.setAdapter(adapter);
    }

    private void getData(Context context) {
        mMaterielDetail = OutStorageUtils.getPickingOutStorageDetailBySerial(context, mSerial);
        addOutStoragePickingItemListData(context, mMaterielDetail, mMaterielDetail.getBatchList(), mBatchNumber, pickingOutStorageDetailList);
    }

    public interface OutStoragePickingDetailListener {

        void getDefaultBatch(String number);

        void selectItem(int position);

    }

    @Override
    public void onBackPressed() {
        String message = check();
        if (message != null) {
            DialogUtils.getConfirmDialog(OutStoragePickingDetailActivity.this, message, new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    OutStoragePickingDetailActivity.super.onBackPressed();
                }
            }).show();
        } else {
            updateInfo(OutStoragePickingDetailActivity.this);
            OutStoragePickingDetailActivity.super.onBackPressed();
        }
    }

    private void updateInfo(Context context) {
        mMaterielDetail.setBatchList(adapter.getmBatchList());
        mMaterielDetail.setActualSendNumber(adapter.getmActualSendNumber());
        OutStorageUtils.updatePickingOutStorageDetail(context, mMaterielDetail);
    }

    private String check() {
        String message;
        message = checkCount();
        if (message != null) {
            return message;
        }
        message = checkConfirm();
        if (message != null) {
            return message;
        }
        message = checkNumber();
        if (message != null) {
            return message;
        }
        message = checkCanSendNumber();
        if (message != null) {
            return message;
        }
        return message;
    }

    private String checkCanSendNumber() {
        Double actualSendNumberDou = Double.parseDouble(adapter.getmActualSendNumber());
        Double canSendNumberDou = Double.parseDouble(mMaterielDetail.getCanSendNumber());
        if (actualSendNumberDou > canSendNumberDou) {
            return "实发数量不能大于可发数量，请修改\n是否强制退出?";
        }
        return null;
    }

    private String checkCount() {
        if (adapter == null || adapter.getCount() <= 1) {
            return "请添加记录\n是否强制退出?";
        }
        return null;
    }

    private String checkConfirm() {
        int count = adapter.getCount() - 1;
        for (int i = 0; i < count; i++) {
            OutStoragePickingBatchListBean.DataBean.BatchListBean batchItem = (OutStoragePickingBatchListBean.DataBean.BatchListBean) adapter.getItem(i);
            if (!PickingOutStorageDetailListAdapter.CONFIRM.equals(batchItem.getIsConfirm())) {
                return "第" + (i + 1) + "条记录没确认\n是否强制退出?";
            }
        }
        return null;
    }

    private String checkNumber() {
        int count = adapter.getCount() - 1;
        for (int i = 0; i < count; i++) {
            OutStoragePickingBatchListBean.DataBean.BatchListBean batchItem = (OutStoragePickingBatchListBean.DataBean.BatchListBean) adapter.getItem(i);
//            LogUtils.i("batchItem.getOutStorageNumber():" + batchItem.getOutStorageNumber());
            if (TextUtils.isEmpty(batchItem.getOutStorageNumber()) || RegexUtils.checkZero(batchItem.getOutStorageNumber())) {
                return "第" + (i + 1) + "条记录没有数量或值非法\n是否强制退出?";
            }
        }
        return null;
    }
}
